package com.ruoyi.notice.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.notice.domain.SysMaterial;
import com.ruoyi.notice.domain.SysNotices;
import com.ruoyi.notice.service.SysMaterialService;
import com.ruoyi.notice.service.SysNoticesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author YL
 * @version 1.0
 * @date 2020/6/23 13:45
 */
@RestController
@RequestMapping("material")
public class SysMaterialController extends BaseController {

    @Autowired
    private SysMaterialService sysMaterialService;

    @Autowired
    private SysNoticesService sysNoticesService;
    /**
     * 查询${tableComment}
     */
    @GetMapping("get/{id}")
    public SysMaterial get(@PathVariable("id") Long id) {
        return sysMaterialService.selectSysMaterialById(id);

    }

    /**
     * 查询物料列表
     */
    @GetMapping("list")
    public R list(SysMaterial sysMaterial) {
        startPage();
        return result(sysMaterialService.selectSysMaterialList(sysMaterial));
    }


    /**
     * 新增保存物料
     */
    @PostMapping("save")
    public R addSave(@RequestBody SysMaterial sysMaterial) {
        int rows=sysMaterialService.insertSysMaterial(sysMaterial);
        if (rows==-1){
            return R.error(400,"物料号已存在！");
        }
        return toAjax(rows);
    }

    /**
     * 修改保存物料
     */
    @PostMapping("update")
    public R editSave(@RequestBody SysMaterial sysMaterial) {
        return toAjax(sysMaterialService.updateSysMaterial(sysMaterial));
    }

    /**
     * 删除${tableComment}
     */
    @PostMapping("removes")
    public R removes(String ids) {
        return toAjax(sysMaterialService.deleteSysMaterialByIds(ids));
    }

    @PostMapping("remove")
    public R remove(Long id) {
        List<String> materials=sysNoticesService.selectMaterial();
        String sysMaterial=sysMaterialService.selectSysMaterialById(id).getMaterialName();
        boolean flag=true;
        for (String material : materials){
            if (sysMaterial.equals(material)){
                flag=false;
                break;
            }
        }
        if (!flag){
            return R.error(400,"禁止删除已被关联的物料号！");
        }
        return toAjax(sysMaterialService.deleteSysMaterialById(id));
    }
}
